/**
 * @ParseViewConfig.as
 * 
 * @author sodaChen mail:asframe@163.com
 * @version 1.0
 * <br>Copyright (C), 2010 asFrame.com
 * <br>This program is protected by copyright laws.
 * <br>Program Name:AStruts2
 * <br>Date:2012-4-9
 */
package com.asframe.astruts2.config
{
	import com.asframe.astruts2.beans.MediatorBean;
	import com.asframe.astruts2.beans.NoticeMethodBean;
	import com.asframe.astruts2.beans.ResBean;
	import com.asframe.astruts2.beans.ViewBean;
	import com.asframe.ref.RefConts;
	import com.asframe.utils.RefUtils;
	import com.asframe.utils.StringUtils;

	/**
	 * 解析单个View的配置文件
	 * @author sodaChen
	 * @Date:2012-4-9
	 */
	public class ParseViewConfig
	{
		private var defaultView:ViewBean;
		private var isDefault:Boolean;
		
		public function ParseViewConfig()
		{
			defaultView = new ViewBean();
			//布置默认值
			defaultView.parent = "root";
			defaultView.initEvent = "addedToStage";
			defaultView.source = "root";
			defaultView.x = 9999;
			defaultView.y = 9999;
			defaultView.singleton = true;
			defaultView.lazy = true;
			defaultView.autoInit = true;
			defaultView.group = "astruts2";
			defaultView.undoSite = true;
		}
		/**
		 * 解析资源配置 
		 * @param reses
		 * @return 
		 * 
		 */		
		public function parseRes(reses:XMLList):Array
		{
			if(reses.length() > 0)
			{
				var resAry:Array = new Array();
				for each(var res:XML in reses)
				{
					resAry.push(createResBean(res));
				}
				return resAry;
			}
			return null;
		}
		public function xmlToViewBeans(ary:Array,xmlList:XMLList):void
		{
			for each (var viewXml:XML in xmlList) 
			{
				ary.push(parseViewConfig(viewXml));
			}
		}
		public function parseViewConfig(viewXml:XML,isDefault:Boolean = false):ViewBean
		{
			this.isDefault = isDefault;
			var viewBean:ViewBean = new ViewBean();
			
			viewBean.name = viewXml.@name;
			getPropertyValue(viewXml.@swap,"swap",viewBean,RefConts.BOOLEAN_TYPE);
			getPropertyValue(viewXml.@tab,"tab",viewBean,RefConts.BOOLEAN_TYPE);
			getPropertyValue(viewXml.@x,"x",viewBean,RefConts.NUMBER_TYPE);
			getPropertyValue(viewXml.@y,"y",viewBean,RefConts.NUMBER_TYPE);
			getPropertyValue(viewXml.@visible,"visible",viewBean,RefConts.BOOLEAN_TYPE);
			getPropertyValue(viewXml.@singleton,"singleton",viewBean,RefConts.BOOLEAN_TYPE);
			getPropertyValue(viewXml.@source,"source",viewBean,RefConts.STRING_TYPE);
			getPropertyValue(viewXml.@lazy,"lazy",viewBean,RefConts.BOOLEAN_TYPE);
			getPropertyValue(viewXml.@clazz,"clazz",viewBean,RefConts.STRING_TYPE);
			getPropertyValue(viewXml.@initEvent,"initEvent",viewBean,RefConts.STRING_TYPE);
			getPropertyValue(viewXml.@autoInit,"autoInit",viewBean,RefConts.BOOLEAN_TYPE);
			getPropertyValue(viewXml.@drag,"drag",viewBean,RefConts.BOOLEAN_TYPE);
			getPropertyValue(viewXml.@alone,"alone",viewBean,RefConts.BOOLEAN_TYPE);
			getPropertyValue(viewXml.@undoSite,"undoSite",viewBean,RefConts.BOOLEAN_TYPE);
			getPropertyValue(viewXml.@parent,"parent",viewBean,RefConts.STRING_TYPE);
			getPropertyValue(viewXml.@loading,"loading",viewBean,RefConts.STRING_TYPE);
			getPropertyValue(viewXml.@undoSite,"undoSite",viewBean,RefConts.BOOLEAN_TYPE);
			getPropertyValue(viewXml.@depth,"depth",viewBean,RefConts.INT_TYPE);
			getPropertyValue(viewXml.@group,"group",viewBean,RefConts.STRING_TYPE);
			getPropertyValue(viewXml.@mutexViews,"mutexViews",viewBean,RefConts.STRING_TYPE);
			getPropertyValue(viewXml.@mutexGroups,"mutexGroups",viewBean,RefConts.STRING_TYPE);
			
			//解析mediator
			viewBean.mediators = new Array();
			
			for each(var mediatorXml:XML in viewXml.mediator)
			{
				var mediatorBean:MediatorBean = new MediatorBean();
				mediatorBean.viewName = viewBean.name;
				mediatorBean.name = mediatorXml.@name;
				mediatorBean.clazz = mediatorXml.@clazz;
				mediatorBean.view = mediatorXml.@view;
				//对感兴趣的字符串进行一些处理，例如去除空格等特殊字符
				var notices:Array = mediatorXml.notices.toString().split(",");
				for (var i:int = 0; i < notices.length; i++) 
				{
					//过滤掉空格
					notices[i] = StringUtils.trim(notices[i]);
				}
				//方法级别
				var noticeMethodList:XMLList = mediatorXml.elements("method");
				if(noticeMethodList != null && noticeMethodList.length() > 0)
				{
					var ary:Array = [];
					ConfigParse.xmlToObj(ary,noticeMethodList,NoticeMethodBean);
				}
				mediatorBean.notices = notices;
				viewBean.mediators.push(mediatorBean);
			}
			viewBean.reses = parseRes(viewXml.res);
			if(isDefault)
			{
				defaultView = viewBean;
			}
			return viewBean;
		}
		private function createResBean(res:XML):ResBean
		{
			var bean:ResBean = new ResBean();
			bean.tip = res.@tip;
			bean.path = res.@path;
			return bean;
		}
		private function getPropertyValue(value:String,pro:String,viewBean:ViewBean,type:String = "String"):void
		{
			if(value == "")
			{
				viewBean[pro] = defaultView[pro];
			}
			else
			{
				viewBean[pro] = RefUtils.changeStringValue(value,type);
			}
		}
	}
}
